package com.wteam.Service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wteam.PoJo.DO.Goods;
import com.wteam.PoJo.DTO.PageQuery;
import com.wteam.PoJo.VO.GoodsVO;
import com.wteam.mapper.GoodsMapper;
import com.wteam.Service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wteam.until.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-07-16
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Result<List<GoodsVO>> selectgood(PageQuery pageQuery) {
        String key = pageQuery.getPage() + "_" + pageQuery.getPageSize()
                + "_" + pageQuery.getSortBy() + "_" + pageQuery.getIsAsc();

        //查缓存
        List<GoodsVO> goodsVOS1 = (List<GoodsVO>) redisTemplate.opsForHash().get("GOOD", key);

        if (CollUtil.isNotEmpty(goodsVOS1)){
            return Result.success(goodsVOS1);
        }
        Page<Goods> objectPage = pageQuery.toMpPage();
        Page<Goods> page = lambdaQuery().page(objectPage);
        List<Goods> records = page.getRecords();
        if (CollUtil.isEmpty(records)) {
            return Result.success();
        }
        List<GoodsVO> goodsVOS = BeanUtil.copyToList(records, GoodsVO.class);
        //缓存redis
        redisTemplate.opsForHash().put("GOOD", key, goodsVOS);
        return Result.success(goodsVOS);
    }


}
